


var map;

function init() {

	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(local);
	}
	createMap();
	setTimeout("loadMarkers()",1000);
}



function createMap(){
	
	var x = 0;
	var y = 0;
	var z = 1;
	
	if(lat!="null" && lng!=null) {
		x = lng;
		y = lat;
		z = zoom;
	}
	
    var latlng = new google.maps.LatLng (y,x);
    var myOptions = { zoom: z,
                      center: latlng,
                      mapTypeId: google.maps.MapTypeId.TERRAIN
                    };
	
    map = new google.maps.Map(document.getElementById("map"), myOptions); 
}


function loadMarkers() {
	var bbox = map.getBounds();  	
  	makeRequest("loadMarkers.jsp?bbox="+bbox.toUrlValue());
	setTimeout("loadMarkers()",3000);
}

var markers = [];

function drawMarkers(txt) {
	
	var index = 0;
	for(index = 0; index < markers.length; index++)   {
		markers[index].setMap(null);
	}
	markers = [];
	
	if(trim(txt).length < 2)
		return;
	
	var token = trim(txt).split(",");
	index = 0;
	for(var i=0; i<token.length; i=i+5) {
		markers[index] = createMarker(token[i],token[i+1],token[i+2],token[i+3],token[i+4]);
		index ++;
	}
}

function trim(str) {
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}



function clickMarker(marker) {
	var x = marker.getTitle().split("__");
	window.location = "result.jsp?state_input_city="+x[0]+"&state_input_name="+x[1]+"&activity="+x[2]+"#map";
}

function local(position) {
	if(lat == null && lng == null) {
		var latlng = new google.maps.LatLng (position.coords.latitude,position.coords.longitude);
		map.setCenter(latlng);
		map.setZoom(8);
	}
}



function createMarker(lat,lon,city,state,activity){
 var pos = new google.maps.LatLng (lat,lon);
 var marker = new google.maps.Marker({position: pos, map: map});  
 marker.setIcon("icons/"+activity+".png");
 
 
 marker.setTitle(city+"__"+state+"__"+activity);
 google.maps.event.addListener(marker, 'click', function() {clickMarker(marker)});
 return marker;
}